package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SaleBill;
import com.ruoyi.common.sine.domain.SaleBillInfo;
import com.ruoyi.system.mapper.SaleBillMapper;
import com.ruoyi.system.service.ISaleBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sine.req.SaleReq;
import sine.resp.SaleResp;

import java.math.BigDecimal;
import java.util.*;

/**
 * SaleBillService
 *
 * @author HanQuinn
 * Date: 2023/5/8
 * Time: 11:14
 */
@Service
public class SaleBillServiceImpl implements ISaleBillService {

    @Autowired
    private SaleBillMapper saleBillMapper;


    @Override
    public SaleBill query() {
        return saleBillMapper.query();
    }

    @Override
    public SaleResp togetherBill(SaleReq saleReq) {
        String[] split = saleReq.getBillCodes().split(",");
        List<SaleBill> saleBills = saleBillMapper.getByBillCodes(split);
        SaleResp resp = new SaleResp();
        List<SaleBillInfo> infos = new ArrayList<>();
        HashMap<String, SaleBillInfo> map = new HashMap<>();
        saleBills.forEach(o -> {
            infos.addAll(o.getInfo());
            resp.set_widget_1670487290393(o.get_widget_1670487290393());
            resp.set_widget_1671184499366(o.get_widget_1671184499366());
            resp.set_widget_1671432749581(o.get_widget_1671432749581());
            resp.set_widget_1671432749584(o.get_widget_1671432749584());
            resp.set_widget_1673581156871(o.get_widget_1673581156871());
            resp.set_widget_1673849292399(o.get_widget_1673849292399());
            resp.setAddressInfo(o.getAddressInfo());
        });
        infos.forEach(o -> {
            String key = o.get_widget_1671433763850();
            if (map.containsKey(key)) {
                SaleBillInfo saleBillInfo = map.get(key);
                o.set_widget_1673846188680(new BigDecimal(o.get_widget_1673846188680()).add(new BigDecimal(saleBillInfo.get_widget_1673846188680())).toString());
                o.set_widget_1671067664695(new BigDecimal(o.get_widget_1671067664695()).add(new BigDecimal(saleBillInfo.get_widget_1671067664695())).toString());
                o.set_widget_1673371384913(new BigDecimal(o.get_widget_1673371384913()).add(new BigDecimal(saleBillInfo.get_widget_1673371384913())).toString());
                o.set_widget_1673406693654(new BigDecimal(o.get_widget_1673406693654()).add(new BigDecimal(saleBillInfo.get_widget_1673406693654())).toString());
                o.set_widget_1673406693655(new BigDecimal(o.get_widget_1673406693655()).add(new BigDecimal(saleBillInfo.get_widget_1673406693655())).toString());
                o.set_widget_1673371950371(new BigDecimal(o.get_widget_1673371950371()).add(new BigDecimal(saleBillInfo.get_widget_1673371950371())).toString());
                o.set_widget_1673367776151(new BigDecimal(o.get_widget_1673367776151()).add(new BigDecimal(saleBillInfo.get_widget_1673367776151())).toString());
                o.set_widget_1673421742661(new BigDecimal(o.get_widget_1673421742661()).add(new BigDecimal(saleBillInfo.get_widget_1673421742661())).toString());
                o.set_widget_1673416086627(new BigDecimal(o.get_widget_1673416086627()).add(new BigDecimal(saleBillInfo.get_widget_1673416086627())).toString());
                o.set_widget_1671068715478(new BigDecimal(o.get_widget_1671068715478()).add(new BigDecimal(saleBillInfo.get_widget_1671068715478())).toString());
                o.set_widget_1671068715479(new BigDecimal(o.get_widget_1671068715479()).add(new BigDecimal(saleBillInfo.get_widget_1671068715479())).toString());
                o.set_widget_1671433138631(new BigDecimal(o.get_widget_1671433138631()).add(new BigDecimal(saleBillInfo.get_widget_1671433138631())).toString());
                o.set_widget_1671067664697(new BigDecimal(o.get_widget_1671067664697()).add(new BigDecimal(saleBillInfo.get_widget_1671067664697())).toString());
                BigDecimal freeFee = new BigDecimal(o.get_widget_1671433138631());
                BigDecimal originFee = new BigDecimal(o.get_widget_1673421742661());
                String frrRate = freeFee.divide(originFee, 4, BigDecimal.ROUND_HALF_UP).toString();
                o.set_widget_1671433138638(frrRate);

            }
            map.put(key, o);
        });
        List<SaleBillInfo> infosResp = new ArrayList<>(map.values());
        infosResp.forEach(o -> {
            Integer count = Integer.valueOf(o.get_widget_1671067664695());
            Integer boxRule = Integer.valueOf(o.get_widget_1671414524606());
            // 件数
            o.set_widget_1673513366666(String.valueOf(count / boxRule));
            // 零头数
            o.set_widget_1673513366662(String.valueOf(count % boxRule));
        });
        resp.setInfo(infosResp);
        return resp;
    }
}
